package com.yunhe.abnormal.repository.data;

import com.yunhe.abnormal.domain.data.AbnormalEventRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
public interface AbnormalEventRecordRepository extends JpaRepository<AbnormalEventRecord, Long> {
    /**
     * 查询全部异常事件记录
     *
     * @param spec     查询条件
     * @param pageable 分页对象
     */
    Page<AbnormalEventRecordRepository> findAll(Specification<AbnormalEventRecordRepository> spec, Pageable pageable);

    /**
     * 查询全部异常事件记录
     *
     * @param spec 查询条件
     * @param sort 排序对象
     */
    Iterable<AbnormalEventRecordRepository> findAll(Specification<AbnormalEventRecordRepository> spec, Sort sort);

    /**
     * 查询全部异常事件记录
     *
     * @param spec 查询条件
     */
    Iterable<AbnormalEventRecordRepository> findAll(Specification<AbnormalEventRecordRepository> spec);

    @Transactional
    @Modifying
    @Query(value = "delete from data_abnormal_event_record where abnormal_event_id=:eventId", nativeQuery=true)
    void deleteByEventId(@Param("eventId") Long eventId);

    @Query(value = "select * from data_abnormal_event_record where abnormal_event_id=:eventId order by dtime asc limit 1", nativeQuery=true)
    AbnormalEventRecord findByAbnormalEventId(@Param("eventId") Long eventId);
}
